package com.qianniu.napi.admin.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Coupon;
import com.qianniu.napi.admin.entity.CouponTpl;
import com.qianniu.napi.admin.service.ICouponService;
import com.qianniu.napi.admin.service.ICouponTplService;
import com.qianniu.napi.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

@Configuration
@Component // 此注解必加
@EnableScheduling // 此注解必加
public class CouponTask {

    private static final Logger logger = LoggerFactory.getLogger(CouponTask.class);

    @Autowired
    private ICouponService couponService;
    @Autowired
    private ICouponTplService couponTplService;

    //更新优惠券过期状态
    //1、修复endtime，如果为空，重新设置endtime
    //2、判断是否过期
    //3、按照天【yyyy-mm-dd】来判断过期
    @Scheduled(cron = "0 15 1 * * ?")
    public void checkCouponState() {
        logger.debug("----------------------------------------CouponTask checkCouponState start");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Integer index = 1;
        Integer limit = 100;
        boolean flag = true;
        while (flag) {
            Wrapper<Coupon> wrapper = new EntityWrapper<Coupon>().eq("state", 1)
                    .lt("is_forever", 1).lt("is_outdate", 1);

            Page page = new Page(index, limit);
            Page<Coupon> list = couponService.selectPage(page, wrapper);

            for (Coupon item : list.getRecords()) {
                //修复endtime，如果为空，重新设置endtime
                if (Strings.isNullOrEmpty(item.getEndtime())) {
                    CouponTpl tpl = couponTplService.selectById(item.getTplid());
                    logger.debug("--------------------CouponTask 11111");
                    try {
                        item.setEndtime(DateUtil.addToday(item.getStarttime(), tpl.getExpire()));
                    } catch (Exception e) {
                        logger.debug("--------------------CouponTask 11111 error");
                    }
                }

                // 判断是否过期
                if (Strings.isNullOrEmpty(item.getEndtime())) continue;
                logger.debug("--------------------CouponTask 2222");

                String today = DateUtil.getToday();
                try {
                    //按照天【yyyy-mm-dd】来判断过期
                    if (sdf.parse(today).getTime() > sdf.parse(item.getEndtime()).getTime()) {
                        item.setIsOutdate(1);
                        item.setState(-1);
                        item.updateById();
                        logger.debug("--------------------CouponTask 333");
                    }
                    logger.debug("--------------------CouponTask 333444");
                } catch (Exception e) {
                    logger.debug("--------------------error");
                }
            }
            flag = list.getRecords() != null && list.getRecords().size() == limit;
            logger.debug("--------------------CouponTask 888 flag=" + flag);
            index++;
        }

        logger.debug("--------------------CouponTask checkCouponState exe success");
    }

}
